package nccloud.impl.arap.guarantee.rule;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.ArrayUtils;

import nc.impl.pubapp.pattern.rule.IRule;
import nc.vo.arap.guaranteecost.AggGuaranteeCostVO;
import nc.vo.arap.guaranteecost.GuaranteeCostBVO;
import nc.vo.pub.VOStatus;

public class SaveBeforeRule implements IRule<AggGuaranteeCostVO> {

	@Override
	public void process(AggGuaranteeCostVO[] vos) {
		for(AggGuaranteeCostVO vo: vos) {
			processSingle(vo);
		}
	}

	private void processSingle(AggGuaranteeCostVO vo) {
		List<GuaranteeCostBVO> newBvo = new ArrayList<GuaranteeCostBVO>();
		
		GuaranteeCostBVO[] bodyvos = (GuaranteeCostBVO[]) vo.getChildrenVO();
		
		if(ArrayUtils.isNotEmpty(bodyvos)) {
			for(GuaranteeCostBVO tempvo : bodyvos) {
				if(vo.getPrimaryKey() == null) {
					if(tempvo.getStatus() != VOStatus.DELETED) {
						newBvo.add(tempvo);
					}
				}
			}
			
			if(newBvo.size() > 0) {
				GuaranteeCostBVO[] newBvos = new GuaranteeCostBVO[newBvo.size()];
				newBvo.toArray(newBvos);
				vo.setChildrenVO(newBvos);
			}
		}
	}
}
